package com.inspur.ourhis_afterend.Controller;

import com.inspur.ourhis_afterend.Entity.User;
import com.inspur.ourhis_afterend.Service.UserService;
import com.inspur.ourhis_afterend.Tools.PageAction;
import com.inspur.ourhis_afterend.Tools.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

@Api("用户模块接口")
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    UserService userService;

    // 登录验证
    @ApiOperation("登录验证")
    @PostMapping("/login")
    @ApiParam(value = "传入一个用户名和密码")
    public Result login(@Param("username") String username,
                        @Param("password") String password) throws Exception {
        User user = userService.findByUsername(username);
        if (user != null) {
            if (user.getPassword().equals(password)) {
                return Result.success(new String("登陆成功"));
            }
        }
        return Result.failure("登录失败");
    }

    // 用户添加
    @ApiOperation("添加用户")
    @PostMapping("/add")
    @ApiParam(value = "传入一个用户对象")
    public Result add(@RequestBody User user,
                      @Param("age") Integer age) {
        System.out.println(age);
        System.out.println(user);
        try {
            userService.add(user);
            return Result.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }

    // 用户删除
    @ApiOperation("用户删除")
    @PostMapping("/del")
    @ApiParam(value = "传入一个用户id")
    public Result del(@Param("id") Integer id) {
        try {
            userService.del(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }

    // 用户修改
    @ApiOperation("用户信息修改")
    @ApiParam(value = "传入一个用户对象")
    @PostMapping("/edit")
    public Result edit(@RequestBody User user) {
        try {
            return Result.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }

    // 检验是否有重复用户名
    @ApiOperation("检验是否有重复用户名")
    @PostMapping("/checkName")
    @ApiParam(value = "传入一个用户名")
    public Result checkName(@Param("username") String username) throws Exception {
        User user = userService.findByUsername(username);
        if (user != null){
            return Result.failure("用户名重复");
        } else {
            return Result.success("用户名可用");
        }
    }

    // 查询全部用户
    @ApiOperation("查询全部用户")
    @PostMapping("/findAll")
    @ApiParam(value = "传入分页对象")
    public Result findAll(@Param("pageNo") Integer pageNo,
                          @Param("pageSize") Integer pageSize) {
        PageAction pageAction = new PageAction(pageNo, pageSize);
        System.out.println(pageAction);
        try {
            return Result.success(userService.findAll(pageAction));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }

    /**
     * 查询单个用户  用于表单回显
     * @param id
     * @return
     */
    @PostMapping("/findOne")
    @ApiOperation("查询单个用户")
    @ApiParam(value = "传入一个用户id")
    public Result findOne(@Param("id") Integer id) {
        System.out.println(id);
        try {
            return Result.success(userService.findOne(id));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }

    /**
     * 通过用户名查询单个用户
     * @param realName
     * @return
     */
    @PostMapping("/findByName")
    @ApiOperation("通过用户名查询单个用户")
    @ApiParam(value = "传入一个用户姓名")
    public Result findByName(@Param("realName") String realName) {
        System.out.println(realName);
        try {
            return Result.success(userService.findByName(realName));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }


    /**
     * 通过用户类型查询多个用户
     * @param role
     * @return
     */
    @ApiOperation("通过用户类型查询多个用户")
    @ApiParam(value = "传入一个用户类型")
    @PostMapping("/findByRole")
    public Result findByRole(@Param("role") String role) {
        System.out.println(role);
        try {
            return Result.success(userService.findByRole(role));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }
}
